package gui.productgroup;

import gui.common.Controller;
import gui.common.IView;
import gui.common.SizeUnits;
import gui.inventory.ProductContainerData;
import model.entity.container.ProductContainer;
import model.entity.container.ProductGroup;
import model.management.ProductGroupManager;

import common.Result;
import common.Size;

/**
 * Controller class for the add product group view.
 */
public class AddProductGroupController extends Controller implements IAddProductGroupController
{

  private final ProductContainer container;
  private ProductGroup           pg;

  /**
   * Constructor.
   * 
   * @param view
   *          Reference to add product group view
   * @param container
   *          Product container to which the new product group is being added
   */
  public AddProductGroupController(IView view, ProductContainerData container)
  {
    super(view);
    this.container = container.getTag();
    construct();
  }

  //
  // Controller overrides
  //

  /**
   * Returns a reference to the view for this controller.
   * 
   * {@pre None}
   * 
   * {@post Returns a reference to the view for this controller.}
   */
  @Override
  protected IAddProductGroupView getView()
  {
    return (IAddProductGroupView) super.getView();
  }

  /**
   * Sets the enable/disable state of all components in the controller's view. A component should
   * be enabled only if the user is currently allowed to interact with that component.
   * 
   * {@pre None}
   * 
   * {@post The enable/disable state of all components in the controller's view have been set
   * appropriately.}
   */
  @Override
  protected void enableComponents()
  {
    try
    {
      String name = getView().getProductGroupName();
      String supplyStr = getView().getSupplyValue();
      float supply;
      if (supplyStr.isEmpty())
        supply = 0;
      else
        supply = Float.parseFloat(getView().getSupplyValue());

      SizeUnits unit = getView().getSupplyUnit();
      Size size = new Size(supply, unit);

      pg = new ProductGroup(name, container, size);
      Result result = ProductGroupManager.validateAddProductGroup(pg);
      getView().enableOK(result.getStatus());
    }
    catch (Exception e)
    {
      getView().enableOK(false);
    }
  }

  /**
   * Loads data into the controller's view.
   * 
   * {@pre None}
   * 
   * {@post The controller has loaded data into its view}
   */
  @Override
  protected void loadValues()
  {
    getView().setSupplyValue("0");
  }

  //
  // IAddProductGroupController overrides
  //

  /**
   * This method is called when any of the fields in the add product group view is changed by the
   * user.
   */
  @Override
  public void valuesChanged()
  {
    enableComponents();
  }

  /**
   * This method is called when the user clicks the "OK" button in the add product group view.
   */
  @Override
  public void addProductGroup()
  {
    ProductGroupManager.addProductGroup(pg);
  }
}
